package com.kanshu.ksgb.fastread.doudou.base.baseui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 具有分页效果的基础fragment
 * Created by wcy on 2017\7\13 0013.
 */

public abstract class BaseListFragment<T> extends BaseFragment implements IGenrialMvpView<BaseResult<List<T>>> {

    public RecyclerView mRecylerView;
    public EmptyLayout mEmptyLayout;
    public TwinklingRefreshLayout mSwipeRefresh;
    protected List<T> mList = new ArrayList<>();
    protected BaseQuickAdapter<T> mAdapter;
    protected PageRequestParams mRequestParams = new PageRequestParams();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_common_refresh_rv, container, false);
        mRecylerView = view.findViewById(R.id.recyler_view);
        mEmptyLayout = view.findViewById(R.id.empty_layout);
        mSwipeRefresh = view.findViewById(R.id.swipe_refresh);
        init();
        return view;
    }

    protected void init() {
        mAdapter = provideAdapter();
        mEmptyLayout.setRetryListener(() -> {
            showLoading(null);
            mRequestParams.page = 1;
            getContentAysnc(mRequestParams);
        });
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mRequestParams.page = 1;
            getContentAysnc(mRequestParams);
        });
        mSwipeRefresh.setEnableRefresh(isSwipeRefreshEnable());
        initRecyclerView();
        mAdapter.setRequestDataListener(() -> getContentAysnc(mRequestParams));

    }

    @Override
    public void showContent(BaseResult<List<T>> listBaseResult) {
        if (mRequestParams.page == 1) {
            if (Utils.isEmptyList(listBaseResult.result.data) && mList.size() <= 0) {
                if (mEmptyLayout == null) {
                    return;
                }
                showEmptyByCode(Constants.ErrCode.NoData);
                SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
                return;
            }
            mList.clear();
            mAdapter.setIsNoMoreData(false);
        }
        mList.addAll(listBaseResult.result.data);
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (mRequestParams.page >= listBaseResult.result.total_page) {
            mAdapter.noMoreData();
        } else {
            mRequestParams.page++;
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (Utils.isEmptyList(mList)) {
            showEmptyByCode(errCode);
        } else {
            dismissLoading();
        }
    }

    @Override
    public void showLoading(String s) {
        mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
    }

    @Override
    public void dismissLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    protected abstract BaseQuickAdapter provideAdapter();

    protected abstract void getContentAysnc(PageRequestParams params);

    protected boolean isSwipeRefreshEnable() {
        return true;
    }

    protected void initRecyclerView() {
        RecyclerViewHelper.initRecyclerViewV(getActivity(), mRecylerView, false, mAdapter);
    }

    public void showEmptyByCode(int code) {
        int status = EmptyLayout.STATUS_NO_NET;
        if (code == Constants.ErrCode.NoData) {
            status = EmptyLayout.STATUS_NO_DATA;
        }
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(status);
        }
    }
}
